<template>
  <div class="app-container">
    <!-- 上部分表单 -->
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="68px"
      label-position="center"
      class="globalSerchFormStyle"
      style="margin: 10px 0px 15px 0px"
    >
      <el-form-item
        v-if="['', null, '7'].includes($store.state.user.userLevel)"
        label="油企名称"
        prop="enterpriseId"
        size="small"
        style="padding-top: 4px"
      >
        <el-select
          v-model="queryParams.enterpriseId"
          :clearable="true"
          filterable
          placeholder="请选择油企"
          style="width: 260px; margin-left: 10px"
          @change="handleSelectChange"
        >
          <el-option
            v-for="(item, index) in enterpriseList"
            :key="index"
            :value="item.enterpriseId"
            :label="item.enterpriseName"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item
        label="油站名称"
        prop="stationId"
        clearable
        size="small"
        style="padding-top: 4px"
      >
        <el-select
          v-model="queryParams.stationId"
          :clearable="true"
          filterable
          placeholder="请选择油站"
          style="width: 260px; margin-left: 10px"
        >
          <el-option
            v-for="item in stationList"
            :key="item.stationId"
            :label="item.stationName"
            :value="item.stationId"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item
        label="营业时间"
        prop="createdDate"
        size="small"
        style="padding-top: 4px"
      >
        <el-date-picker
          size="small"
          style="width: 260px; margin-left: 10px"
          v-model="dateRangeCreatedDate"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="changeDateRangeDate"
        >
        </el-date-picker>
      </el-form-item>

      <div class="button-from"></div>
    </el-form>
    <el-row class="global_el_rowStyle" :gutter="10">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-search"
          @click="handleQuery"
          size="mini"
          >搜索</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery"
          plain
          >重置</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          icon="el-icon-download"
          @click="handleExport"
          size="mini"
          plain
          >导出</el-button
        ></el-col
      >
    </el-row>

    <div class="summary-container">
      <div class="summary-item f1">
        <div class="summary-item-box f1">
          <span class="summary-title">应收总金额</span>
          <span class="summary-sum">{{ workSum.receivableAmt }}元</span>
        </div>
        <div class="summary-item-box f1">
          <span class="summary-title">走字数合计</span>
          <span class="summary-sum">{{ workSum.totalWordCount }}</span>
        </div>
      </div>
      <div class="summary-item f1">
        <div class="summary-item-box f1">
          <span class="summary-title">实收总金额</span>
          <span class="summary-sum">{{ workSum.amt }}元</span>
        </div>
        <div class="summary-item-box f1">
          <span class="summary-title">损溢合计</span>
          <span class="summary-sum">0 L/kg</span>
        </div>
      </div>
      <div class="summary-item f1">
        <div class="summary-item-box f1">
          <span class="summary-title">优惠总金额</span>
          <span class="summary-sum">{{ workSum.discountAmt }}元</span>
        </div>
        <div class="summary-item-box f1">
          <span class="summary-title"></span>
          <span class="summary-sum"></span>
        </div>
      </div>
      <div class="summary-item f1">
        <div class="summary-item-box f1">
          <span class="summary-title">退款总金额</span>
          <span class="summary-sum">{{ workSum.refundAmt }}元</span>
        </div>

        <div class="summary-item-box f1">
          <span class="summary-title"></span>
          <span class="summary-sum"></span>
        </div>
      </div>
    </div>

    <!-- 表格 -->
    <el-table style="width: 100%" :data="orderList" stripe v-loading="loading">
      <af-table-column
        label="油企名称"
        align="left"
        prop="enterpriseName"
        v-if="['', null].includes($store.state.user.userLevel)"
      />
      <af-table-column label="油站名称" align="left" prop="stationName" />
      <af-table-column label="营业时间" align="left" prop="timeOnTrade" />
      <af-table-column label="班次" align="left" width="140">
        <template slot-scope="scope">
          <span>{{ scope.row.classesDayOrNight || 0 }}</span>
        </template>
      </af-table-column>

      <af-table-column label="走字数合计" align="left" width="140">
        <template slot-scope="scope">
          <span>{{ scope.row.totalWordCount || 0 }}</span>
        </template>
      </af-table-column>
      <af-table-column label="应收总金额" align="left" width="140">
        <template slot-scope="scope">
          <span>{{ scope.row.receivableAmt || 0 }}</span>
        </template>
      </af-table-column>
      <af-table-column label="优惠总金额" align="left" width="140">
        <template slot-scope="scope">
          <span>{{ scope.row.discountAmt || 0 }}</span>
        </template>
      </af-table-column>
      <af-table-column label="实收总金额" align="left" width="140">
        <template slot-scope="scope">
          <span>{{ scope.row.amt || 0 }}</span>
        </template>
      </af-table-column>
      <af-table-column
        fixed="right"
        label="操作"
        align="left"
        class-name="small-padding fixed-width"
        width="200px"
      >
        <!--fixed="right"-->
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            disable
            @click="
              goNewPage('/overview/Overview_ShiftDayReport_See', scope.row)
            "
            >查看明细</el-button
          >
        </template>
      </af-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>
<script>
import { timeComparison } from "@/utils/timeConversion";
import { enterpriseList } from "@/api/station/orderHistory";
import { listEnterpriseStation } from "@/api/dept";
import {
  classReportList,
  classReportSum,
  exportClassReport,
} from "@/api/overview";
export default {
  name: "ShiftDayReport",
  data() {
    return {
      enterpriseList: [],
      stationList: [],
      //时间间隔值
      dateRangeCreatedDate: [],
      // 总条数
      total: 0,
      // 统计数据
      workSum: {},
      //表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        enterpriseId: null,
        stationId: null,
        // 开始时间
        beginTime: null,
        // 截止时间
        endTime: null,
      },
      isOnlyOneStation: false, // 当前油企是否只有一个正常使用的油站
      loading: false,
    };
  },
  created() {
    this.getList();
    this.getEnterpriseList();
    // 油企
    if (this.presentEnterprise && this.presentEnterprise.enterpriseId) {
      this.queryParams.enterpriseId = this.presentEnterprise.enterpriseId;
      this.getStationList(this.queryParams.enterpriseId);
    }
    // 油站账号登录
    if (this.presentSation) {
      this.queryParams.stationId = this.presentSation;
      this.handleQuery();
    }
  },

  methods: {
    // 选择了时间
    changeDateRangeDate() {
      // 开始时间
      this.queryParams.beginTime = this.dateRangeCreatedDate[0];
      // 截止时间
      this.queryParams.endTime = this.dateRangeCreatedDate[1];
    },
    handleSelectChange(value) {
      this.stationList = [];
      this.queryParams.stationId = null;
      this.getStationList(value);
    },
    // 查询油企
    getEnterpriseList() {
      enterpriseList().then((res) => {
        this.enterpriseList = res.rows;
      });
    },
    // 查询油站
    getStationList(val) {
      listEnterpriseStation({ enterpriseId: val, status: "1" }).then((res) => {
        this.stationList = res;
        // 判断单站，多站
        if (res.length <= 1) {
          this.isOnlyOneStation = true;
        }
        if (res.length === 1) {
          this.queryParams.stationId = res[0].stationId;
          // this.handleQuery();
        }
      });
    },

    goNewPage(url, row) {
      this.$router.push({
        path: url,
        query: {
          stationId: row.stationId,
          stationName: row.stationName,
          timeOnTrade: row.timeOnTrade,
          classesDayOrNight: row.classesDayOrNight,
        },
      });
    },

    // 搜索列表
    getList() {
      this.loading = true;

      // const time = timeComparison(
      //   "earching",
      //   this.queryParams.beginTime,
      //   this.queryParams.endTime
      // );
      // if (!time) return (this.loading = false);

      classReportList(this.queryParams).then((response) => {
        this.orderList = response.rows;
        this.total = response.total;
      });

      classReportSum(this.queryParams).then((res) => {
        this.workSum = {
          receivableAmt: res.receivableAmt === null ? 0 : res.receivableAmt, // 应收总金额
          totalWordCount: res.totalWordCount === null ? 0 : res.totalWordCount, // 走字数合计
          amt: res.amt === null ? 0 : res.amt, // 实收总金额
          discountAmt: res.discountAmt === null ? 0 : res.discountAmt, // 优惠总金额
          refundAmt: res.refundAmt === null ? 0 : res.refundAmt, // 退款总金额
        };
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRangeCreatedDate = [];
      // 开始时间
      this.queryParams.beginTime = null;
      // 截止时间
      this.queryParams.endTime = null;
      this.resetForm("queryForm");
      this.getList();
    },

    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;

      // const time = timeComparison(
      //   "export",
      //   queryParams.beginTime,
      //   queryParams.endTime
      // );
      // if (!time) return;

      this.$confirm("是否确认导出所有数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportClassReport(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        });
    },
  },
};
</script>
<style lang="scss" scoped>
.app-container {
  margin-top: -15px;
}

.button-from {
  width: 100%;
  display: flex;
  flex-wrap: wrap;

  .button-style1 {
    display: inline-block;
    width: 80px;

    .el-button {
      margin: 0 0 0 15px;
    }
  }
}

::v-deep .card-table {
  padding: 10px;
  margin-top: 0 !important;
  background-color: #f8f8f8;

  .el-button {
    margin: 0 5px 0 0;
  }
}

// 表格样式
::v-deep .el-form-item {
  margin-bottom: 0;

  .el-form-item__label {
    padding: 0;
    margin: 0;
    height: 29px;
    font-weight: 400;
    font-size: 12px;
  }

  .el-button--cyan {
    margin-center: 20px;
  }
}

::v-deep .el-table {
  border: 1px #f8f8f8 solid;
  border-bottom: 1px #dfe6ec solid;
}

::v-deep .is-center {
  border: 1px transparent solid;
  border-bottom: 1px #f8f8f8 solid;
}

.summary-container {
  width: 100%;
  border: 1px solid #e7e3e3;
  padding: 10px 5px;
  border-radius: 10px;

  margin-bottom: 25px;
  display: flex;
  flex-direction: row;

  .summary-item {
    display: flex;
    flex-direction: column;

    border-right: 1px dashed rgba(0, 0, 0, 0.1);

    .summary-item-box {
      border-bottom: 1px dashed rgba(0, 0, 0, 0.1);
      padding: 5px;
      display: flex;
      justify-items: center;
      align-items: center;
      flex-direction: column;

      .summary-title {
        font-size: 18px;
      }

      .summary-sum {
        font-size: 24px;
        font-weight: 700;
      }
    }
  }

  .summary-item:last-child {
    border-right: none;
  }

  .summary-item-box:nth-last-child(-n + 1) {
    border-bottom: none;
  }
}

.f1 {
  flex: 1;
}
</style>
